﻿using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel.Common;
using Net5CommonLib.SystemLib.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Net5ApiAndEfMySql.Repository
{
    public abstract class BaseRepository
    {
        
        ///// <summary>
        ///// soa_system_center 链接的属性,  这样写可能有bug, 因为如果没有上下文,无法获取生命周期为Scoped的注入对象
        ///// </summary>
        //public SoaSystemCenterDbContext SoaSystemCenterDbContext
        //{
        //    //通过DiManager来获取di容器的SoaSystemCenter链接
        //    get { return DiManager.GetDiObjScoped<SoaSystemCenterDbContext>(); }
        //}
        /// <summary>
        /// 公共的分页方法
        /// </summary>
        /// <typeparam name="P">查询结果的类型</typeparam>
        /// <param name="source">搜索条件</param>
        /// <param name="search">搜索model</param>
        /// <returns></returns>
        public PagerResult<P> Pager<P>(IQueryable<P> source, SearchModel search) where P : class, new()
        {
            int total = source.Count();
            PagerResult<P> result = new PagerResult<P>()
            {
                PageCount = (int)Math.Ceiling((decimal)total / search.limit),
                recordsFiltered = total,
                recordsTotal = total,
                data = source.Skip(search.start).Take(search.limit).ToList(),
                PageIndex = search.page
            };
            return result;
        }

    }
}
